''''
数据库相关操作
'''''
import pymysql


def connect(mysqldb):
    try:
        conn = pymysql.connect(user=mysqldb['user'],
            password=mysqldb['pwd'],
            host=mysqldb['host'],
            database=mysqldb['dbname'],
            port=mysqldb['port'],
            charset="utf8")
        print(f"连接数据库成功") #
        #Logger().info(f"连接数据库成功")
        return conn
    except Exception as e:
        print(f"连接数据库失败，异常信息为：{e}")
        #Logger().error(f"连接数据库失败，异常信息为：{e}")


def close(conn):
    try:
        conn.close()
        print(f"断开数据库连接成功")
        #Logger().info(f"断开数据库连接成功")
    except Exception as e:
        print(f"断开数据库连接失败，异常信息为：{e}")
        #Logger().error(f"断开数据库连接失败，异常信息为：{e}")

def execute(conn, sql):
    try:
        c = conn.cursor()
        c.execute(sql)
        conn.commit()
        print(f"执行{sql}语句成功")
        #Logger().info(f"执行{sql}语句成功")
    except Exception as e:
        print(f"执行{sql}语句失败，异常信息为：{e}")
        #Logger().error(f"执行{sql}语句失败，异常信息为：{e}")

# 根据手机号删除用户
def delete_user(db, phone):
    a = connect(db)
    execute(a,f"delete from member where mobilephone = '{phone}';")
    close(a)

# 根据手机号查询用户
def select_user(db, phone):
    a = connect(db)
    c = a.cursor()
    c.execute(f"select * from member where mobilephone = '{phone}';")
    result = c.fetchone()
    close(a)
    return result




